import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/admin",
    name: "Admin",
    component: () => import("@/views/Admin.vue"),
    children: [
      {
        path: "login",
        name: "AdminLogin",
        component: () => import("@/views/admin/Login.vue"),
        meta: { requiresAuth: true },
        // beforeEnter: (to, from, next) => {
        //   console.log(to);
        //   console.log(from);
        //   if (!localStorage.getItem("userinfo")) {
        //     next();
        //   } else {
        //     next({ name: "Admin" });
        //   }
        // },
      },
      {
        path: "about",
        name: "AdminAbout",
        component: () => import("@/views/admin/About.vue"),
      },
      {
        path: "article",
        name: "AdminArticle",
        component: () => import("@/views/admin/article/Article.vue"),
      },
      {
        path: "article/new",
        name: "AdminArticleNew",
        component: () => import("@/views/admin/article/ArticleNew.vue"),
      },
      {
        path: "article/:article_id",
        name: "AdminArticleDetail",
        component: () => import("@/views/admin/article/ArticleDetail.vue"),
        props: true,
      },
    ],
  },
  /*
	/admin
	/admin/article
	/admin/article/:id
	/admin/article/new
	/admin/article/:id/update
	/admin/banner
	/admin/banner/:id
	*/
  {
    path: "/about",
    name: "About",
    component: () => import("../views/About.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  linkActiveClass: "active-blue",
  linkExactActiveClass: "active-orange",
});

router.beforeEach((to, from, next) => {
  /*
	const userinfo = localStorage.getItem("userinfo");
  console.log(userinfo);
  console.log("to:", to);
  console.log("form:", from);
  if (to.name === "Admin") {
    if (!userinfo) {
      next({ name: "AdminLogin" });
    }
    next();
  }*/
  next();
});

router.afterEach(() => {
  // console.log("to:", to);
  // console.log("form:", from);
});

export default router;
